package com.i9he.ping.sms;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumQueryRequest;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumQueryResponse;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

public class TaobaoSms implements SmsSender, SmsQuery {

	private String url;

	private String appKey;

	private String secret;

	private String extend;

	private String smsType;

	private String smsFreeSignName;

	private String smsTemplateCode;

	private Integer pageSize = 10;

	@Override
	public void send(String recNum, String message)  throws Exception {
		TaobaoClient client = new DefaultTaobaoClient(url, appKey, secret);

		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setExtend(extend);
		req.setSmsType(StringUtils.defaultString(smsType, "normal"));
		req.setSmsFreeSignName(smsFreeSignName);
		req.setSmsParamString(message);
		req.setRecNum(recNum);
		req.setSmsTemplateCode(smsTemplateCode);

		AlibabaAliqinFcSmsNumSendResponse rsp;

		rsp = client.execute(req);
		System.out.println(rsp.getBody());

	}

	@Override
	public String queryForJson(String recNum, Date date, int page) throws Exception {
		final SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		TaobaoClient client = new DefaultTaobaoClient(url, appKey, secret);

		AlibabaAliqinFcSmsNumQueryRequest req = new AlibabaAliqinFcSmsNumQueryRequest();
		// req.setBizId("1234^1234");
		req.setRecNum(recNum);
		req.setQueryDate(format.format(date));
		req.setCurrentPage(Long.valueOf(page));
		req.setPageSize(Long.valueOf(pageSize));

		AlibabaAliqinFcSmsNumQueryResponse rsp;
		rsp = client.execute(req);
		String responseBody = rsp.getBody();
		return responseBody;
		
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setAppKey(String appKey) {
		this.appKey = appKey;
	}

	public void setSecret(String secret) {
		this.secret = secret;
	}

	public void setExtend(String extend) {
		this.extend = extend;
	}

	public void setSmsType(String smsType) {
		this.smsType = smsType;
	}

	public void setSmsFreeSignName(String smsFreeSignName) {
		this.smsFreeSignName = smsFreeSignName;
	}

	public void setSmsTemplateCode(String smsTemplateCode) {
		this.smsTemplateCode = smsTemplateCode;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

}
